goodsEditWindow = Ext.extend(Ext.Window,{
		closable:true,
		title: '商品信息',
		width:400,
		layout:'fit',
		createPanel:function() {
			var goods_status = Ext.data.Record.create([
               {name:'goods_status_id',type:'string'},
			   {name:'name',type:'string'}           
			   ]);
			var storeStatus = new Ext.data.Store({
				/*proxy: new Ext.data.HttpProxy({
					 url:"index.php?controller=Terminallevel&action=ToJSON"
				}),
				
				record:
				// create reader that reads the project records
				reader: new Ext.data.JsonReader({},[
					 {name:'terminal_level_id',type:'string'},
					 {name:'name',type:'string'}
				]),*/
				proxy: new Ext.data.MemoryProxy(jsCfgData['Id2Discription']['GoodsStatus']),
				reader: new Ext.data.JsonReader({
				   id:'goods_status_id'
				   }, goods_status),
				autoLoad:true
			});    
		   

		 storeStatus.load();
			return new Ext.FormPanel({
				//id:'editform',
				frame:true,
				//url:'index.php',
				labelWidth: 90,
				defaults: {width: 140},	
				defaultType: 'textfield',
				autoHeight: true,
				//activeItem:1,
				items: [{
							xtype:'hidden',
							fieldLabel: '商品ID',
							name: 'goods_id'
						},{
							fieldLabel: '商品条码',
							name: 'barcode',
							allowBlank:true
							//blankText: ''
						},{
							fieldLabel: '所属终端',
							name: 'terminal_id',
							id:'terminal_id',
							allowBlank:false,
							blankText: '终端编号必须输入！',
							listeners:{focus:function(){
								var terminalTitleRecord = Ext.data.Record.create([
									{name:'terminal_id',	type:'int'		},
									{name:'name',type:'string'}
									
								]);

								var ds = new Ext.data.JsonStore({
									url:'index.php?controller=Terminal&action=boList',
									root: 'Result',
									autoLoad:true,
									fields: terminalTitleRecord	
								});

								var colModel = new Ext.grid.ColumnModel([
									{header: "终端编号",		width: 130,	 dataIndex: 'terminal_id'},
									{header: "终端名称",	width: 100,	 dataIndex: 'name'}	
								
								]);

								var GP=new Ext.grid.GridPanel({
									id:'terminal_list_grid',
									ds: ds,
									cm: colModel,
									sm: new Ext.grid.RowSelectionModel({
										singleSelect: true
										
									}),
									
									height: 325,
									
									border: true
									
								});
								var thiswindow = new Ext.Window({
									iconCls:'my-edit-icon',
									closeable:true,
									title:'选择终端',
									width:400,
									height:500,
									layout:'fit',
									modal:true,
									items:[
										GP
											
									],
									buttons:[
									{text:'选择',
										handler:function(btn,e){
											var nowSel = Ext.getCmp("terminal_list_grid").getSelectionModel();
											if (nowSel.getCount()!=0){
							
												Ext.getCmp('terminal_id').setValue(nowSel.getSelected().data['terminal_id']);
												btn.ownerCt.close();
						    
											}else{
												Ext.Msg.alert('注意','请在列表中选择终端');
											}
										}
									},{text:'取消',
									handler:function(btn,e){
										btn.ownerCt.close();
									}}
									]
								});
							    thiswindow.show();
							}}
						},{
							fieldLabel: '商品名称',
							name: 'name',
							allowBlank:false,
							blankText: '商品名称必须输入！'
						},{
							fieldLabel: '价格',
							name: 'price',
							allowBlank:false,
							value:0,
							blankText: '价格必须输入！'
						},{
							fieldLabel: '积分系数',
							name: 'factor',
							allowBlank:true,
							value:1
							//blankText: ''
						},
						 {
							 xtype:'combo',
							 fieldLabel: '商品状态',
							 name:'status',
							 lazyRender:true,
							 store: storeStatus,
							 displayField:'name',
							 valueField: 'goods_status_id',
							 hiddenName:'status',
							 typeAhead: true,
							 mode: 'local',
							 triggerAction: 'all',
							 emptyText:'请选择商品状态',
							 value:1,
							 selectOnFocus:true,
							 editable:false
							 
                         },{
							fieldLabel: '数量',
							name: 'totalnum',
							value:1
						},{
							xtype:'checkbox',
							fieldLabel: '是否关联库存',
							name: 'associate',
							value:1,
							align:'left'
							//y:5
						},{
							xtype:'textarea',
							width:240,
							fieldLabel: '备注',
							name: 'memo'
						}]

			});//end return new Ext.FormPanel
		},
		initComponent:function() {  
			goodsEditWindow.superclass.initComponent.call(this);  
			this.wp = this.createPanel();  
			this.add(this.wp);  
		}
	});//end difine EditWindow

goods_list = Ext.extend(Ext.Panel,{
	id:'goods_list',  
	title:'商品列表',  
	//closeAction:'hide',
	autoScroll:true,  
	closable:true,
	layout:'fit', //如果用函数来创建该面板的话,布局必须设置为fit,否则不会显示该面板中的内容

    createPanel:function() {
		var btedit = new  Ext.Toolbar.Button({
				text:'修改',
				icon:'images/icons/fam/icons/application_edit.png',
				cls:'x-btn-text-icon',
				handler:function(){
						var nowSel = Ext.getCmp("goods-list-grid").getSelectionModel();
						if (nowSel.getCount()!=0){
							var window_id = 'editwindowsform'+nowSel.getSelected().data['goods_id'];
							var thiswindow = Ext.getCmp(window_id);

							if (!thiswindow){
								var thiswindow = new goodsEditWindow({
										id:window_id,
										iconCls:'my-edit-icon',
										_record:nowSel.getSelected(),
										buttons:[{
													text:'保存',
													handler:function(btn,e){
														var myform = btn.ownerCt.findByType('form')[0].getForm();
														if(myform.isValid()){
															btn.disabled=true;
															myform.doAction('submit',{
																url:'index.php?controller=Goods&action=Modify',
																method:'post',
																params:'',
																waitMsg:'正在通讯...',
																waitTitle:'请等待',
																success:function(form,action){
																	Ext.Msg.alert('操作',action.result.data);
																	myform.updateRecord(btn.ownerCt._record);
																	btn.disabled=false;
																	btn.ownerCt.close();
																},
																failure:function(){
																	Ext.Msg.alert('操作','保存失败！');
																	btn.disabled=false;
																}	
															});
													   }
													}
												},{
													text:'取消',
													handler:function(btn,e){
														btn.ownerCt.close();
													}
												}]
									});//end var editwindows
								//thiswindow.findByType('form')[0].getForm().url = 'index.php';
							}//end if
							thiswindow.show();
							thiswindow.findByType('form')[0].getForm().loadRecord(nowSel.getSelected());
							
						}else{
							Ext.Msg.alert('注意','请在列表中选择要编辑的商品');
						}
					}//end handler
		});//end var btedit

		var btdelete = new Ext.Toolbar.Button({
				text:'删除',
				icon:'images/icons/fam/icons/delete.png',
				cls:'x-btn-text-icon',
				handler:function(btn,e){
						var nowSel = Ext.getCmp("goods-list-grid").getSelectionModel();
						if (nowSel.getCount()!=0){
							
							Ext.MessageBox.confirm('删除', '你是否真的要删除?', function(btn){
								if (btn=='yes'){
									var myform = Ext.getCmp("goods-form").form;
									if(myform.isValid()){
										btn.disabled=true;
										myform.doAction('submit',{
											url:'index.php?controller=Goods&action=Delete',
											method:'post',
											params:{goods_id:nowSel.getSelected().data['goods_id']},
											waitMsg:'正在通讯...',
											waitTitle:'请等待',
											success:function(form,action){
												Ext.Msg.alert('操作',action.result.data);
												//myform.updateRecord(btn.ownerCt._record);
												ds.remove(nowSel.getSelected());
												btn.disabled=false;
											},
											failure:function(){
												Ext.Msg.alert('操作','删除失败！');
												btn.disabled=false;
											}	
										});
									}
								}
						    });
						}else{
							Ext.Msg.alert('注意','请在列表中选择要删除的商品');
						}
					}
			});//end var btdelete
		var btadd = new Ext.Toolbar.Button({
				text:'添加',
				icon:'images/icons/fam/icons/add.png',
				cls:'x-btn-text-icon',
				handler:function(){
					var thiswindow = new goodsEditWindow({
							iconCls:'my-add-icon',
							buttons:[{
										text:'添加',
										handler:function(btn,e){
											var myform = btn.ownerCt.findByType('form')[0].getForm();
											if(myform.isValid()){
												btn.disabled=true;
												myform.doAction('submit',{
													url:'index.php?controller=Goods&action=Add',
													method:'post',
													params:'',
													waitMsg:'正在通讯...',
													waitTitle:'请等待',
													success:function(form,action){
														
														if (typeof(action.result)!='undefined'){
															Ext.Msg.alert('操作','添加成功');
															var myNewRecord = new GoodsTitleRecord(Ext.decode(action.result.data)[0]);
															ds.add(myNewRecord);
															btn.ownerCt.close();
														}else{
															Ext.Msg.alert('操作','添加失败！');
														}
														
														//Ext.Msg.alert('操作','成功');

														btn.disabled=false;
													},
													failure:function(){
														Ext.Msg.alert('操作','添加失败！');
														btn.disabled=false;
													}	
												});
										   }
										}
									},{
										text:'取消',
										handler:function(btn,e){
											btn.ownerCt.close();
										}
									}]
						});//end var editwindows
						thiswindow.show();
				}//end handler
			});//end var btadd
		var GoodsTitleRecord = Ext.data.Record.create([
				{name:'goods_id',	type:'int'		},
				{name:'barcode',			type:'string'	},
				{name:'terminal_id',	type:'string'		},
				{name:'name',		type:'string'	},
				{name:'price',			type:'float'	},
				{name:'factor',   type:'float'},
				{name:'status',				type:'int'		},
				{name:'totalnum',				type:'int'		},
				{name:'associate',				type:'bool'		},
				{name:'memo',		type:'string'	}
				
			]);

		var ds = new Ext.data.JsonStore({
			url:'index.php?controller=Goods&action=list',
			root: 'Result',
			autoLoad:true,
			fields: GoodsTitleRecord	
			});
		// 定制 renderer function 关联库存
		function showAssociate(val){
			if (val==0){
				return '<span style="color:red;">×</span>';
			}else{
				return '<span style="color:green;">√</span>'
			}
		}
		
		// 定制 renderer function 级别显示
		function goodsStatusShow(val){
			var a = jsCfgData['Id2Discription']['GoodsStatus'];
			for (k in a)
			{
				if (a[k]['goods_status_id']==val)
				{
					return a[k]['name'];
				}
			}
			
		}

		// the DefaultColumnModel expects this blob to define columns. It can be extended to provide
		// custom or reusable ColumnModels
		var colModel = new Ext.grid.ColumnModel([
			{header: "商品名称",		width: 75,	sortable: true, dataIndex: 'name',				locked:false, id:'goods-name'},
			{header: "条码",		width: 75,	sortable: true, dataIndex: 'barcode'        	},
			{header: "数量",		width: 75,	sortable: true, dataIndex: 'totalnum'			},
			{header: "价格",		width: 75,	sortable: true, dataIndex: 'price'				},
			{header: "关联库存",	width: 35,	sortable: true, dataIndex: 'associate',		    renderer: showAssociate},
			{header: "积分系数",	width: 85,	sortable: true, dataIndex: 'factor'		},
			{header: "状态",	width: 85,	 dataIndex: 'status',		renderer: goodsStatusShow},
			{header: "备注",	width: 85,	 dataIndex: 'memo'		}
		]);
	
	 //	Here is where we create the Form
	
	
	 
		return new Ext.FormPanel({
			id: 'goods-form',
			frame: true,
			tbar:[btadd,'-',btdelete,'-',btedit],
			bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: ds,
			displayInfo: true,
            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
            emptyMsg: "无显示数据"
            }),
			labelAlign: 'left',
			//title: 'Company data',
			bodyStyle:'padding:5px',
			width: 750,
			layout: 'column',	// Specifies that the items will now be arranged in columns
			//border: true,
			listeners: {
						bodyresize:function( p,w,h ){
							Ext.getCmp('goods-list-grid').setHeight(h-50);
						}
			},
			items: [{
				columnWidth: 1,
				layout: 'fit',
				items: {
					xtype: 'grid',
					id:'goods-list-grid',
					ds: ds,
					cm: colModel,
					sm: new Ext.grid.RowSelectionModel({
						singleSelect: true,
						listeners: {
							rowselect: function(sm, row, rec) {
								
							}
						}
					}),
					autoExpandColumn: 'goods-name',
					height: 325,
					//title:'Company Data',
					border: true,
					listeners: [{
						render: function(g) {
							g.getSelectionModel().selectRow(0);
						},
						delay: 10 // Allow rows to be rendered.
					}]
				}
		
			}]
		});
     },   
    //重装控件初始化函数,在该函数中完成面板中内容的初始化  
    initComponent:function() {  
        goods_list.superclass.initComponent.call(this);  
        this.fp = this.createPanel();  
        this.add(this.fp);  
    }
	
});